package com.summer.ioc.bean.definition.json;

import com.summer.ioc.bean.definition.AbstractBeanDefinition;
import com.summer.ioc.bean.definition.json.configuration.BeanConstructorArgConfiguration;
import com.summer.ioc.bean.definition.json.configuration.BeanPropertyConfiguration;
import com.summer.ioc.util.CollectionUtils;
import com.summer.ioc.util.StringUtils;
import lombok.Getter;
import lombok.Setter;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author xiaoye
 * @create 2021-05-03 21:07
 */
@Setter
@Getter
public abstract class AbstractJsonBeanDefinition extends AbstractBeanDefinition {

    public AbstractJsonBeanDefinition(String name, Class beanClass) {
        super(name, beanClass);
    }

    protected List<BeanConstructorArgConfiguration> constructorArgs;

    protected List<BeanPropertyConfiguration> properties;

    @Override
    public Set<String> getDependencyBeanNames(){
        if (dependencyBeanNames == null)
        {
            dependencyBeanNames = new HashSet<>();
            if (CollectionUtils.hasElement(constructorArgs))
            {
                for (var constructorArg : constructorArgs)
                {
                    if (StringUtils.hasLength(constructorArg.getRef()))
                        dependencyBeanNames.add(constructorArg.getRef());
                }
            }
            if (CollectionUtils.hasElement(properties))
            {
                for (var beanPropertyConfiguration : properties) {
                    if (StringUtils.hasLength(beanPropertyConfiguration.getRef()))
                        dependencyBeanNames.add(beanPropertyConfiguration.getRef());
                }
            }
        }
        return dependencyBeanNames;
    }
}
